
<template>
  <div>
    <el-row style="margin-top: 20px" type="flex" justify="center">
      <el-col :span="23">
        <el-form
          ref="searchForm"
          :model="searchForm"
          label-width="80px"
          :inline="true"
        >
          <el-form-item label="学生姓名" prop="realname">
            <el-input v-model="searchForm.realname"></el-input>
          </el-form-item>
          <el-button size="small" type="primary" @click="init">搜索</el-button>
          <el-button size="small" @click="resetSearch('searchForm')"
            >重置</el-button
          >
        </el-form>
      </el-col>
    </el-row>

    <el-row type="flex" justify="center" style="margin-bottom: 10px">
      <el-col :span="23">
        <el-button
          size="small"
          icon="el-icon-plus"
          type="primary"
          @click="addLeave"
          v-permission="['leave/list/addLeave']"
          >申请请假</el-button
        >
        <!-- <el-button
          size="small"
          icon="el-icon-minus"
          type="danger"
          @click="deleteUsers"
          >批量删除</el-button
        > -->
      </el-col>
    </el-row>

    <el-row type="flex" justify="center">
      <el-col :span="23">
        <el-alert
          title
          type="success"
          :closable="false"
          show-icon
          class="alert_bottom"
        >
          已选择
          <span>{{ 0 }}</span> 项
          <a @click="clearSelectAll" style="color: red">清空</a>
        </el-alert>

        <el-table
          :cell-style="cellstyle"
          size="small"
          ref="multipleTable"
          :border="false"
          :data="tableData"
          tooltip-effect="dark"
          style="width: 100%"
          height="500"
          :highlight-current-row="true"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column
            width="120"
            prop="studentName"
            label="学生姓名"
          ></el-table-column>
          <el-table-column
            width="60"
            prop="gender"
            label="性别"
            show-overflow-tooltip
            :formatter="formatter"
          >
          </el-table-column>
          <el-table-column
            width="120"
            prop="phone"
            label="手机号"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column prop="reason" label="请假理由"></el-table-column>
          <el-table-column
            prop="status"
            label="审批状态"
            width="80"
          ></el-table-column>
          <el-table-column
            prop="approveTeacherName"
            label="审批老师"
          ></el-table-column>
          <el-table-column
            prop="leaveStartTime"
            label="请假开始时间"
          ></el-table-column>
          <el-table-column
            prop="leaveEndTime"
            label="请假结束时间"
          ></el-table-column>

          <el-table-column
            prop="rejectReason"
            label="拒绝理由"
          ></el-table-column>
          <el-table-column label="操作" width="200">
            <template slot-scope="scope">
              <el-button v-permission="['leave/list/approve']"  @click="approve(scope.row)" type="primary" size="mini"
                >审批操作</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>

    <el-row type="flex" justify="end">
      <el-col :span="8">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="searchForm.pageNumber"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="searchForm.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </el-col>
    </el-row>

    <el-dialog
      top="6vh"
      :title="titleText"
      :visible.sync="dialogVisible"
      width="40%"
      :before-close="closeDialog"
    >
      <el-row type="flex" justify="center">
        <el-col :span="22">
          <el-form ref="form" :model="form" label-width="100px">
            <el-form-item label="学生姓名">
              <el-input
                v-model="form.studentName"
                style="width: 400px"
                disabled
              ></el-input>
            </el-form-item>
            <el-form-item label="请假理由">
              <el-input
                rows="3"
                v-model="form.reason"
                type="textarea"
                style="width: 400px"
                :disabled="btnBoolean"
              ></el-input>
            </el-form-item>
            <el-form-item label="当前审批账号" v-if="showcurrentUser">
              <el-input
                v-model="form.name"
                style="width: 400px"
                disabled
              ></el-input>
            </el-form-item>

            <el-form-item label="请假时间段">
              <div class="block">
                <el-date-picker
                  :disabled="btnBoolean"
                  style="width: 400px"
                  v-model="leaveValue"
                  value-format="yyyy-MM-dd"
                  type="daterange"
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                >
                </el-date-picker>
              </div>
            </el-form-item>

            <el-form-item label="审批状态" prop="status" v-if="showcurrentUser">
              <el-select
                v-model="form.status"
                placeholder="请选择通过/拒绝"
                @change="changeLeaveType"
              >
                <el-option
                  v-for="item in leaveTypes"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>

            <el-form-item v-if="showreason" label="拒绝理由">
              <el-input
                type="textarea"
                rows="5"
                style="width: 400px"
                v-model="form.rejectReason"
              ></el-input>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
      <el-row type="flex" justify="end" style="margin-top: 20px">
        <el-col :span="7">
          <el-button size="small" @click="closeDialog">取消</el-button>
          <el-button size="small" @click="submit" type="primary"
            >提交</el-button
          >
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import {
  findLeaveByCondition,
  submitLeave,
  submitaddLeave,
} from "@/api/leaveinfo/leaveinfo-api";

export default {
  /* eslint-disable */
  name: "user",
  computed: {
    ...mapGetters(["name", "avatar", "roles", "id"]),
  },
  data() {
    return {
      btnBoolean: true,
      showcurrentUser: false,
      showreason: false,
      leaveValue: [],
      chooseData: [],
      //edit / add
      titleText: "请假信息",
      dialogVisible: false,
      leaveTypes: [
        {
          value: "未审批",
          label: "未审批",
        },
        {
          value: "已审批",
          label: "已审批",
        },
        {
          value: "已拒绝",
          label: "已拒绝",
        },
      ],
      options: [
        {
          value: 1,
          label: "teacher",
        },
        {
          value: 2,
          label: "student",
        },
      ],
      value1: "",

      // page
      total: 0,
      searchForm: {
        realname: "",
        status: "",
        pageNumber: 0,
        pageSize: 10,
      },
      form: {
        name: "",
        phone: "",
        roleIds: [],
        role: "student",
      },
      tableData: [],
    };
  },

  methods: {
    changeLeaveType(value) {
      if (value === "已拒绝") {
        this.showreason = true;
      } else {
        this.showreason = false;
      }
    },
    cellstyle(row, column, rowIndex, columnIndex) {
      let cellStyle;
      switch (row.row.status) {
        case "未审批":
          cellStyle = "color: red";
          break;
        case "已审批":
          cellStyle = "color: green";
          break;
        case "已拒绝":
          cellStyle = "color: red";
          break;
        default:
          cellStyle = "";
      }
      if (row.column.label == "审批状态") {
        return cellStyle;
      }
    },
    formatter(row, column) {
      if (row.gender === 0) {
        return "男";
      } else {
        return "女";
      }
    },
    addLeave() {
      this.btnBoolean = false;
      this.showcurrentUser = false;
      this.leaveValue = [];
      this.form = {};
      this.form.studentName = this.name;
      this.form.studentUserId = this.id;

      this.dialogVisible = true;
    },
    deleteUsers() {},
    clearSelectAll() {},
    handleSelectionChange() {},
    approve(row) {
      this.form = row;
      this.form.name = this.name;
      this.leaveValue = [this.form.leaveStartTime, this.form.leaveEndTime];

      this.btnBoolean = true;
      this.showcurrentUser = true;
      if (this.form.status === "已拒绝") {
        this.showreason = true;
      }
      this.dialogVisible = true;
      //   findChooseClassInfo(this.form).then((response) => {
      //     if (response.msg === "success") {
      //       console.log(response.data);
      //       this.chooseData = response.data;
      //       this.dialogVisible = true;
      //     }
      //   });
    },
    init() {
      findLeaveByCondition(this.searchForm).then((response) => {
        if (response.msg === "success") {
          this.total = response.data.totalElements;
          this.tableData = response.data.content;
        }
      });
    },

    //重置
    resetSearch(formName) {
      this.$refs[formName].resetFields();
      this.init();
    },
    //page
    handleSizeChange(pageSize) {
      this.searchForm.pageSize = pageSize;
      this.init();
    },
    handleCurrentChange(pageNumber) {
      this.searchForm.pageNumber = pageNumber;
      this.init();
    },

    closeDialog() {
      this.dialogVisible = false;
    },

    submit() {
      console.log(this.form);
      if (this.btnBoolean) {
        submitLeave(this.form).then((response) => {
          if (response.msg === "success") {
            this.$message({ message: "提交成功", type: "success" });
            this.closeDialog();
            this.init();
          }
        });
      } else {
        console.log(this.leaveValue);
        this.form.leaveStartTime = this.leaveValue[0];
        this.form.leaveEndTime = this.leaveValue[1];
        submitaddLeave(this.form).then((response) => {
          if (response.msg === "success") {
            this.$message({ message: "提交成功", type: "success" });
            this.closeDialog();
            this.init();
          }
        });
      }
    },
  },

  created() {
    this.init();
    console.log(this.name);
  },
};
</script>

<style scoped>
/* eslint-disable */
.alert_bottom {
  margin-bottom: 20px;
}
/* eslint-disable */
</style>